package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.OrderDetail;
import com.xmy.cultivate.entity.StudentForData;
import com.xmy.cultivate.service.IOrderDetailService;
import com.xmy.cultivate.service.IStudentForDataService;
import com.xmy.cultivate.service.IStudentForDataTypeService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 不同类型，需要排除或参与的数据，如：人教版学员不参与率的计算，待转校学生 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-01-08
 */
@RestController
@RequestMapping("/api/studentForData")
public class StudentForDataController {

    @Autowired
    IStudentForDataService iStudentForDataService;

    @Autowired
    IStudentForDataTypeService iStudentForDataTypeService;

    @Autowired
    IOrderDetailService iOrderDetailService;

    @GetMapping()
    public Result findAll(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "schoolId") Long schoolId,
            @RequestParam(value = "yearPart") Integer yearPart,
            @RequestParam(value = "quarterNum") Integer quarterNum,
            @RequestParam(value = "subjectsId") Long subjectsId,
            @RequestParam(value = "teacherId") Long teacherId,
            @RequestParam(value = "yearClassId") Integer yearClassId,
            @RequestParam(value ="gradeId",required = false) Long gradeId,
            @RequestParam(value = "studentName") String studentName,
            @RequestParam(value = "typeId", defaultValue = "1",required = false) Long typeId,
            @RequestParam(value = "lessonType", required = false, defaultValue = "1") Integer lessonType,
            @RequestParam(value = "studentId", required = false) Long studentId) {

        IPage<StudentForData> iPage = new Page<>(pageNum, pageSize);

        iStudentForDataService.findAll(iPage, schoolId, studentId, subjectsId, yearPart, quarterNum, lessonType, typeId,yearClassId,gradeId,teacherId, studentName);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id) {
        StudentForData studentForData = iStudentForDataService.getById(id);
        return new Result(Code.OK, studentForData, "");
    }

    @PostMapping()
    public Result save(@RequestBody StudentForData studentForData) {
        return null;
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable(value = "id") String id, @RequestBody StudentForData studentForData) {
        return null;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id) {
        iStudentForDataService.removeById(id);
        return new Result(Code.OK, "", "删除成功");
    }


    /**
     * 获取待转校学员
     * @param pageNum
     * @param pageSize
     * @param schoolId
     * @param yearPart
     * @param quarterNum
     * @param subjectsId
     * @param lessonType
     * @param studentName
     * @param type
     * @param studentId
     * @return
     */
    @GetMapping("/getWaitTransterStudent")
    public Result getWaitTransterStudent(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "schoolId") Long schoolId,
            @RequestParam(value = "yearPart") Integer yearPart,
            @RequestParam(value = "quarterNum") Integer quarterNum,
            @RequestParam(value = "subjectsId") Long subjectsId,
            @RequestParam(value = "teacherId") Long teacherId,
            @RequestParam(value = "yearClassId") Integer yearClassId,
            @RequestParam(value ="gradeId",required = false) Long gradeId,
            @RequestParam(value = "studentName") String studentName,
            @RequestParam(value = "type", defaultValue = "1",required = false) Integer type,
            @RequestParam(value = "lessonType", required = false, defaultValue = "1") Integer lessonType,
            @RequestParam(value = "studentId", required = false) Long studentId) {
            IPage<StudentForData> iPage = new Page<>(pageNum, pageSize);
        Long typeId = iStudentForDataTypeService.getIdForType(type);
        iStudentForDataService.findAll(iPage, schoolId, studentId, subjectsId, yearPart, quarterNum, lessonType, typeId,yearClassId,gradeId,teacherId, studentName);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @GetMapping("/getWaitTransterStudentForAdd")
    public Result getWaitTransterStudentForAdd(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value ="schoolId",required = false) Long schoolId,
            @RequestParam(value ="studentId",required = false) Long studentId,
            @RequestParam(value ="subjectsId",required = false) Long subjectsId,
            @RequestParam(value ="yearPart",required = true) Integer yearPart,
            @RequestParam(value ="quarterNum",required = true) Integer quarterNum,
            @RequestParam(value ="lessonType",required = false,defaultValue = "1") Integer lessonType,
            @RequestParam(value ="type",required = false) Long type,
            @RequestParam(value = "teacherId") Long teacherId,
            @RequestParam(value = "yearClassId") Integer yearClassId,
            @RequestParam(value ="gradeId",required = false) Long gradeId,
            @RequestParam(value ="studentName",required = false) String studentName) {
        IPage<StudentForData> iPage = new Page<>(pageNum, pageSize);
        iStudentForDataService.getWaitTransterStudent(iPage,schoolId,studentId,subjectsId,yearPart,quarterNum,lessonType,type,yearClassId,gradeId,teacherId,studentName);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @PostMapping("/saveWaitTransterStudent")
    public Result saveWaitTransterStudent(@RequestBody StudentForData studentForDataParam) {
        for (Long id:studentForDataParam.getIdList()){
            OrderDetail orderDetail = iOrderDetailService.getById(id);
            if (orderDetail != null){
                Long typeId = iStudentForDataTypeService.getIdForType(1);
                QueryWrapper<StudentForData> studentForDataQueryWrapper = new QueryWrapper<>();
                studentForDataQueryWrapper.eq("student_id",orderDetail.getStudentId());
                studentForDataQueryWrapper.eq("subjects_id",orderDetail.getSubjectsId());
                studentForDataQueryWrapper.eq("year_part",orderDetail.getYearPart());
                studentForDataQueryWrapper.eq("quarter_num",orderDetail.getQuarterNum());
                studentForDataQueryWrapper.eq("data_type_id",typeId);
                studentForDataQueryWrapper.last("limit 1");
                StudentForData studentForDataInfo = iStudentForDataService.getOne(studentForDataQueryWrapper);

                StudentForData studentForData = new StudentForData();
                studentForData.setSchoolId(orderDetail.getSchoolId());
                studentForData.setStudentId(orderDetail.getStudentId());
                studentForData.setSubjectsId(orderDetail.getSubjectsId());
                studentForData.setYearPart(orderDetail.getYearPart());
                studentForData.setYearClassId(orderDetail.getYearClassId());
                studentForData.setQuarterNum(orderDetail.getQuarterNum());
                studentForData.setDataTypeId(typeId);
                if (studentForDataInfo == null){
                    iStudentForDataService.save(studentForData);
                }else {
                    studentForData.setId(studentForDataInfo.getId());
                    iStudentForDataService.updateById(studentForData);
                }
            }
        }
        return new Result(Code.OK, "", "添加成功");
    }
}
